package com.hs.color.lamp;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import com.chiemy.cardview.ColorLampFragment;
import com.chiemy.cardview.R;
import com.chiemy.cardview.Utils;

public class ColorCntrlFragment extends Fragment {
	
	private View root;
	private ColorPickerDialog dialog = null; 
	private Context context;
	public static BluetoothChatService mChatService = null;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.context = activity;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = View.inflate(getActivity(), R.layout.color_ctl_frag, null);
		 
//		Intent intent = ((Activity) context).getIntent();
//		mChatService = (BluetoothChatService) intent.getSerializableExtra("BLUETOOTH_CHAT_SERVICE");
		mChatService = ColorLampFragment.mChatService;
		WindowManager manager = ((Activity) context).getWindow().getWindowManager();
		int height = (int) (manager.getDefaultDisplay().getHeight() * 0.7f);
		int width = (int) (manager.getDefaultDisplay().getWidth() * 1.0f);
		ColorPickerView myView = new ColorPickerView(context, height, width, new ColorPickerView.OnColorChangedListener() {

			@Override
			public void colorChanged(int color) {
				// TODO Auto-generated method stub
				//TODO send color
				byte[] mCmdPacket = null;
				byte[] color_array = new byte[4];
				Utils.int4_to_bytearray(color, color_array);
				Utils.swap_byte_array(color_array);
				color_array[0] = (byte) 0xd0;
				//mCmdPacket = SppCommandPacket.getSendSppCtrlPacket(color_array);
				mCmdPacket = SppCommandPacket.getSendPWMCtrlPacket(color_array);
    			{
    				if(mChatService != null)
    				{
    					if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
    					    Toast.makeText(context, "spp not connected", Toast.LENGTH_SHORT).show();
    					    return;
    					}
    					mChatService.write(mCmdPacket);
    					Log.i("ColorCntrlFragment", "spp send command");
    					//Toast.makeText(context, "spp send command", Toast.LENGTH_SHORT).show();
    				}
    			}
			}
			
			
		});
      
		return myView;
		//return root;
	}
}
